package com.flower.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flower.domain.dto.buyer.review.ReviewDto;
import com.flower.domain.entity.Buyer;
import com.flower.domain.entity.Product;
import com.flower.domain.entity.Review;
import com.flower.domain.vo.buyer.ReviewVO;
import com.flower.mapper.BuyerMapper;
import com.flower.mapper.ProductMapper;
import com.flower.mapper.ReviewMapper;
import com.flower.service.ProductService;
import com.flower.service.ReviewService;
import com.flower.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 86178
* @description 针对表【review(商品评价表)】的数据库操作Service实现
* @createDate 2025-03-04 12:26:59
*/
@Service
@RequiredArgsConstructor
public class ReviewServiceImpl extends ServiceImpl<ReviewMapper, Review>
    implements ReviewService{

    private final BuyerMapper buyerMapper;
    private final ProductMapper productMapper;


    @Override
    public List<ReviewVO> getReviewsByProductId(Integer productId) {
        // 1. 获取商品的所有评价
        LambdaQueryWrapper<Review> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Review::getProductId, productId)
              .orderByDesc(Review::getCreateTime);
        List<Review> reviews = this.list(wrapper);

        if (reviews.isEmpty()) {
            return new ArrayList<>();
        }

        // 2. 获取所有评价用户的信息
        List<Integer> buyerIds = reviews.stream()
                .map(Review::getBuyerId)
                .collect(Collectors.toList());
        
        List<Buyer> buyers = buyerMapper.selectBatchIds(buyerIds);
        Map<Integer, Buyer> buyerMap = buyers.stream()
                .collect(Collectors.toMap(Buyer::getBuyerId, buyer -> buyer));

        // 3. 组装VO
        return reviews.stream().map(review -> {
            ReviewVO vo = new ReviewVO();
            BeanUtils.copyProperties(review, vo);
            
            // 设置买家信息
            Buyer buyer = buyerMap.get(review.getBuyerId());
            if (buyer != null) {
                vo.setBuyerName(buyer.getUsername());
                vo.setBuyerAvatar(buyer.getAvatar());
            }
            
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void addReview(ReviewDto reviewDto) {
        Review review = new Review();
        BeanUtils.copyProperties(reviewDto, review);
        
        // 设置买家ID和创建时间
        review.setBuyerId(UserContext.getUserId().intValue());
        review.setCreateTime(LocalDateTime.now());
        
        this.save(review);
    }

    @Override
    public Page<ReviewVO> getUserReviews(Integer page, Integer pageSize) {
        // 构建查询条件
        LambdaQueryWrapper<Review> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Review::getBuyerId, UserContext.getUserId())
               .orderByDesc(Review::getCreateTime);

        // 分页查询
        Page<Review> reviewPage = this.page(new Page<>(page, pageSize), wrapper);

        // 转换为VO
        List<ReviewVO> reviewVOs = reviewPage.getRecords().stream().map(review -> {
            ReviewVO vo = new ReviewVO();
            BeanUtils.copyProperties(review, vo);
            vo.setProductId(vo.getProductId());
            Product product = productMapper.selectById(review.getProductId());
            if (product != null) {
                vo.setProductName(product.getProductName());
                vo.setProductImage(product.getImage());
            }
            return vo;
        }).collect(Collectors.toList());

        // 构建返回结果
        Page<ReviewVO> result = new Page<>();
        BeanUtils.copyProperties(reviewPage, result, "records");
        result.setRecords(reviewVOs);
        return result;
    }
}




